package com.example.administrator.yuejian2.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.yuejian2.R;
import com.example.administrator.yuejian2.RetrofitListener.RetrofitListener;
import com.example.administrator.yuejian2.adapter.ShoppingCartAdapterH;
import com.example.administrator.yuejian2.bean.AddTrainOrderBean;
import com.example.administrator.yuejian2.bean.ShoppingCartBean;
import com.example.administrator.yuejian2.bean.TrainOrderBean;
import com.example.administrator.yuejian2.model.AddTrainOrderModel;
import com.example.administrator.yuejian2.model.MyTrainOrderModel;
import com.example.administrator.yuejian2.model.TrainDetailModel;
import com.example.administrator.yuejian2.model.TrainOrderDetailModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by an on 2017/6/14.
 * 购物车界面
 */
public class ShoppingCartActivity extends BaseActivity implements View.OnClickListener, ShoppingCartAdapterH.CheckInterface {

    Button btnBack;//全选
    CheckBox ckAll;//总额
    TextView tvShowPrice;//结算
    TextView tvSettlement;//编辑
    private RecyclerView recyclerView;
    TextView btnEdit;//tv_edit
    private RetrofitListener<AddTrainOrderBean> listener;
    private ShoppingCartAdapterH shoppingCartAdapter;
    private RetrofitListener<List<TrainOrderBean>> listRetrofitListener;
    private List<TrainOrderBean> shoppingCartBeanList = new ArrayList<>();
    private String id;
    private String user;
    public static int num;
    private SharedPreferences sp;
    private RecyclerView.LayoutManager layoutManager;
    private double totalPrice = 0.00;// 购买的商品总价
    private int totalCount = 0;// 购买的商品总数量

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping);
        initView();
        shoppingCartAdapter = new ShoppingCartAdapterH(this);
        shoppingCartAdapter.setCheckInterface(this);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(shoppingCartAdapter);
        Intent intent = getIntent();
        id = (String) intent.getStringExtra("b_id");
        num = intent.getExtras().getInt("num_id");
        sp = getSharedPreferences("user_id", Context.MODE_PRIVATE);
        user = sp.getString("userid", null);
        listRetrofitListener = new RetrofitListener<List<TrainOrderBean>>() {
            @Override
            public void onSuccess(List<TrainOrderBean> list, int flag) {
                shoppingCartBeanList = list;
                shoppingCartAdapter.getShopdata(list);
            }

            @Override
            public void onFail() {

            }
        };
        listener = new RetrofitListener<AddTrainOrderBean>() {
            @Override
            public void onSuccess(AddTrainOrderBean addTrainOrderBean, int flag) {
                if (addTrainOrderBean.getSuccess().equals("1")) {
                    Toast.makeText(ShoppingCartActivity.this, "订单生成成功", Toast.LENGTH_SHORT).show();
                    Intent intent1 = new Intent(ShoppingCartActivity.this, FragmentActivity.class);
                    startActivity(intent1);
                    finish();
                }
            }

            @Override
            public void onFail() {

            }
        };
        TrainOrderDetailModel trainOrderDetailModel = new TrainOrderDetailModel();
        trainOrderDetailModel.getTrainOrder(id, listRetrofitListener);
    }

    private void initView() {
        recyclerView = findViewById(R.id.shopRecyclerview);
        btnBack = findViewById(R.id.btn_back);
        ckAll = findViewById(R.id.ck_all);
        tvShowPrice = findViewById(R.id.tv_show_price);
        tvSettlement = findViewById(R.id.tv_settlement);
        ckAll.setOnClickListener(this);
        tvSettlement.setOnClickListener(this);
        btnBack.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //全选按钮
            case R.id.ck_all:
                if (shoppingCartBeanList.size() != 0) {
                    if (ckAll.isChecked()) {
                        for (int i = 0; i < shoppingCartBeanList.size(); i++) {
                            shoppingCartBeanList.get(i).setChoosed(true);
                        }
                        shoppingCartAdapter.notifyDataSetChanged();
                    } else {
                        for (int i = 0; i < shoppingCartBeanList.size(); i++) {
                            shoppingCartBeanList.get(i).setChoosed(false);
                        }
                        shoppingCartAdapter.notifyDataSetChanged();
                    }
                }
                statistics();
                break;
            case R.id.tv_settlement: //结算
                lementOnder();
                break;
            case R.id.btn_back:
                finish();
                break;
        }
    }

    /**
     * 单选
     *
     * @param position  组元素位置
     * @param isChecked 组元素选中与否
     */
    @Override
    public void checkGroup(int position, boolean isChecked) {
        shoppingCartBeanList.get(position).setChoosed(isChecked);
        if (isAllCheck())
            ckAll.setChecked(true);
        else
            ckAll.setChecked(false);
        shoppingCartAdapter.notifyDataSetChanged();
        statistics();
    }

    /**
     * 遍历list集合
     *
     * @return
     */
    private boolean isAllCheck() {

        for (TrainOrderBean group : shoppingCartBeanList) {
            if (!group.isChoosed())
                return false;
        }
        return true;
    }


    public void statistics() {
        totalCount = 0;
        totalPrice = 0.00;
        for (int i = 0; i < shoppingCartBeanList.size(); i++) {
            TrainOrderBean shoppingCartBean = shoppingCartBeanList.get(i);
            if (shoppingCartBean.isChoosed()) {
                totalCount++;
                totalPrice = totalPrice + Integer.parseInt(shoppingCartBean.getTrain_price()) * num;
            }
        }
        tvShowPrice.setText("合计:" + totalPrice);
        tvSettlement.setText("结算(" + totalCount + ")");
    }

    private void lementOnder() {
        for (TrainOrderBean bean : shoppingCartBeanList) {
            boolean choosed = bean.isChoosed();
            if (choosed) {
                String shoppingName = bean.getTrain_name();
                int count = num;
                AddTrainOrderModel addTrainOrderModel = new AddTrainOrderModel();
                addTrainOrderModel.getAddTrainOrder(id, user, shoppingName, String.valueOf(count), String.valueOf(count), String.valueOf(totalPrice), "1", listener);
            }
        }
        Toast.makeText(ShoppingCartActivity.this, "总价：" + totalPrice, Toast.LENGTH_SHORT).show();

    }
}
